package inet.app.view.fragment;

import inet.app.R;
import inet.app.common.XoSoConstants;
import inet.app.controller.database.DataBaseHandler;
import inet.app.controller.parser.Parser_Csd;
import inet.app.controller.parser.Parser_Lbt;
import inet.app.controller.parser.Parser_LoXq;
import inet.app.controller.parser.Parser_Lx;
import inet.app.controller.parser.Parser_Sc;
import inet.app.controller.request.Request;
import inet.app.model.object.Csd_Item;
import inet.app.model.object.Lbt_Item;
import inet.app.model.object.ListTinh_Sc_Item;
import inet.app.model.object.LoXq_Item;
import inet.app.model.object.Lx_Item;
import inet.app.model.object.Sc_Item;
import inet.app.model.object.SubSv_Item;
import inet.app.model.util.DateProc;
import inet.app.model.util.GioHoangDaoDAO;
import inet.app.model.util.LuniSolarCalendar;
import inet.app.model.util.MyPreferenceManager;
import inet.app.model.util.ProgressDialog;
import inet.app.model.util.SmartLog;
import inet.app.model.util.Util;
import inet.app.view.activity.BaseFragmentActivity;
import inet.app.view.activity.MyApp;
import inet.app.view.adapter.ListSoiCauAdapter;
import inet.app.view.dialog.LoginDialog;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentVipSoiCau extends BaseFragmentTV {
	private ProgressDialog pdia;
	private ListView listview;
	private ArrayList<ListTinh_Sc_Item> items;
	private ArrayAdapter<ListTinh_Sc_Item> adapter;
	private String[] item_list_sc_mb = { "Soi Cầu", "Cặp Số Đẹp",
			"Lô Bạch Thủ", "Lô Xiên", "Lô Xiên Quay", "Lô Âm Dương",
			"Lô Ngũ Hành", "Dàn Đặc Biệt Ngày", "Dàn Đặc Biệt Tuần" };
	private String[] item_list_sc_tn = { "Soi Cầu", "Cặp Số Đẹp",
			"Lô Bạch Thủ", "Lô Xiên" };
	private TextView tvfotter;
	private String code = "XSTD";
	private String name = "Thủ Đô";
	private Dialog kqsc_dl;
	private String date_current;
	private String strHoangDao = "", strNguhanh = "";
	private DataBaseHandler db;
	private SubSv_Item item_sub;
	boolean isSendSms = false;
	int countsms = 0;
	String nhamang = "";
	int weekofyear, dayofmonth;
	// SoiCau
	private Request request = new Request();
	private ArrayList<Sc_Item> arraylist_sc = new ArrayList<Sc_Item>();
	private ArrayList<Csd_Item> arraylist_csd = new ArrayList<Csd_Item>();
	private ArrayList<Lbt_Item> arraylist_lbt = new ArrayList<Lbt_Item>();
	private ArrayList<Lx_Item> arraylist_lx = new ArrayList<Lx_Item>();
	private ArrayList<LoXq_Item> arraylist_lxq = new ArrayList<LoXq_Item>();
	private Parser_Sc parser_sc = new Parser_Sc();
	private Parser_Csd parser_csd = new Parser_Csd();
	private Parser_Lbt parser_lbt = new Parser_Lbt();
	private Parser_Lx parser_lx = new Parser_Lx();
	private Parser_LoXq parser_lxq = new Parser_LoXq();
	private TextView tvDateDetail, tvNguhanh, tv2socuoiDb, tvBovenhieu,
			tvLoto3so, tvTkbovenhieu, tvtkCau, tvHoangdao;
	private TextView tvCau10ngay, tvCau9ngay, tvCau8ngay, tvCau7ngay,
			tvLuachon;
	private TextView tvLBT, tvChuy;
	private TextView tvInfo, tvTenLo;
	private TextView tvXien2, tvXien3, tvXien4;
	private TextView tvDkVip;
	private Button btCancel;
	private MyPreferenceManager mPreference;
	private boolean isFree;

	public static Fragment newInstance(Context context) {
		FragmentVipSoiCau f = new FragmentVipSoiCau();
		return f;
	}

	public static Fragment newInstance(Context context, String position) {
		FragmentVipSoiCau f = new FragmentVipSoiCau();
		Bundle args = new Bundle();
		args.putString("pos", position);
		f.setArguments(args);
		return f;
	}

	public static Fragment newInstance(Context context, String position,
			String json, String code) {
		System.out.println("RUn here ==== ...>>");
		FragmentVipSoiCau f = new FragmentVipSoiCau();
		Bundle args = new Bundle();
		args.putString("pos", position);
		args.putString("json", json);
		args.putString("code", code);
		f.setArguments(args);
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(
				R.layout.fragment_vip_soicau, null);
		listview = (ListView) root.findViewById(R.id.listview);
		pdia = new ProgressDialog(getActivity());
		date_current = DateProc.Timestamp2DDMMYYYY(DateProc.createTimestamp());
		mPreference = MyPreferenceManager.getInstance(mActivity
				.getApplicationContext());
		isFree = mPreference.isFree();
		nhamang = Util.getSharePreferences(getActivity());
		// Load animation
		tvfotter = (TextView) root.findViewById(R.id.tvfotter);

		final Animation text_animation = AnimationUtils.loadAnimation(
				getActivity(), R.anim.custom_text_animation);
		tvfotter.startAnimation(text_animation);

		tvfotter.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pdia.show();
				Intent myWebLink = new Intent(
						android.content.Intent.ACTION_VIEW);
				myWebLink.setData(Uri.parse("http://m.ole.vn/"));
				pdia.dismiss();
				startActivity(myWebLink);
			}
		});
		// CAN CHI
		String[] bem = date_current.split("/");
		int[] am = LuniSolarCalendar.Solar2Lunar(Integer.parseInt(bem[0]),
				Integer.parseInt(bem[1]), Integer.parseInt(bem[2]));
		int ngayALLichNgay = am[0];
		int thangALLichNgay = am[1];
		int namALLichNgay = am[2];
		int leapLichNgay = am[3];

		String[] canChiLichNgay = LuniSolarCalendar.CanChi(ngayALLichNgay,
				thangALLichNgay, namALLichNgay, leapLichNgay);
		String ngayCan = canChiLichNgay[0];
		String ngayChiLichNgay = canChiLichNgay[1];
		String thangCan = canChiLichNgay[2];
		String thangChi = canChiLichNgay[3];
		String namCan = canChiLichNgay[4];
		String namChi = canChiLichNgay[5];
		GioHoangDaoDAO hoangDaoDAO = new GioHoangDaoDAO();
		String[] gioHD = hoangDaoDAO.gio_Hoang_Dao_Wap(ngayChiLichNgay);

		strNguhanh = "Ngày " + ngayCan + " " + ngayChiLichNgay + " ,Tháng "
				+ thangCan + " " + thangChi + " ,Năm " + namCan + " " + namChi;
		for (int i = 0; i < gioHD.length; i++) {
			strHoangDao += gioHD[i] + ", ";
		}
		strHoangDao = strHoangDao.substring(0, strHoangDao.length() - 2) + ".";

		System.out.println("KET QUẢ ================= >> " + strHoangDao);
		String gio = LuniSolarCalendar.hourSolar2Lunar(16);
		System.out.println("KET QUa + ===" + gio);
		// END CAN CHI

		// Get Position
		if (getArguments() != null) {
			System.out.println("CHAY VAO 1");
			// json = getArguments().getString("json");
			code = getArguments().getString("code");
		} else {
			System.out.println("cHAY VAO 2");
		}

		pdia.show();
		// Load item soicau
		loadItemList("XSTD");
		pdia.dismiss();

		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int positon, long id) {
				pdia.show();
				code = ((MyApp) mApplication).getCodeTV();
				System.out.println("MA TINH LA ======>> " + code);
				if (MyPreferenceManager.getInstance(getActivity()).getMember() != null) {
					if (code.equals("XSTD")) {
						switch (positon) {
						case 0:
							KqSc_Dialog(getActivity(), "Soi Cầu");
							break;
						case 1:
							KqSc_Dialog(getActivity(), "Cặp Số Đẹp");
							break;
						case 2:
							KqSc_Dialog(getActivity(), "Lô Bạch Thủ");
							break;
						case 3:
							KqSc_Dialog(getActivity(), "Lô Xiên");
							break;
						case 4:
							KqSc_Dialog(getActivity(), "Lô Xiên Quay");
							break;
						case 5:
							KqSc_Dialog(getActivity(), "Lô Âm Dương");
							break;
						case 6:
							KqSc_Dialog(getActivity(), "Lô Ngũ Hành");
							break;
						case 7:
							KqSc_Dialog(getActivity(), "Dàn Đặc Biệt Ngày");
							break;
						case 8:
							KqSc_Dialog(getActivity(), "Dàn Đặc Biệt Tuần");
							break;
						default:
							break;
						}
					} else {
						switch (positon) {
						case 0:
							KqSc_Dialog(getActivity(), "Soi Cầu");
							break;
						case 1:
							KqSc_Dialog(getActivity(), "Cặp Số Đẹp");
							break;
						case 2:
							KqSc_Dialog(getActivity(), "Lô Bạch Thủ");
							break;
						case 3:
							KqSc_Dialog(getActivity(), "Lô Xiên");
							break;
						default:
							break;
						}
					}
					kqsc_dl.show();
				} else {
					// Util.showDialog("Bạn chưa đăng nhập, Mời đăng nhập để sử dụng tính năng !",
					// getActivity());
					LoginDialog
							.newInstance(mActivity)
							.setLoginListener(mActivity.mLoginListener)
							.show(mActivity
									.getSupportFragmentManager(),
									"login_dialog");
					pdia.dismiss();
				}
			}
		});
		return root;
	}

	public void loadItemList(String code) {

		if (code.equals("XSTD"))
			items = getItems();
		else
			items = getItemsTN();

		adapter = new ListSoiCauAdapter(getActivity(), items);
		listview.setAdapter(adapter);
	}

	private ArrayList<ListTinh_Sc_Item> getItems() {
		ArrayList<ListTinh_Sc_Item> list = new ArrayList<ListTinh_Sc_Item>();
		for (int i = 0; i < item_list_sc_mb.length; i++) {
			list.add(get(item_list_sc_mb[i]));
		}
		return list;
	}

	private ArrayList<ListTinh_Sc_Item> getItemsTN() {
		ArrayList<ListTinh_Sc_Item> list = new ArrayList<ListTinh_Sc_Item>();

		for (int i = 0; i < item_list_sc_tn.length; i++) {
			list.add(get(item_list_sc_tn[i]));
		}
		return list;
	}

	private ListTinh_Sc_Item get(String name) {
		return new ListTinh_Sc_Item(name);
	}

	public void KqSc_Dialog(Context c, String select) {
		kqsc_dl = new Dialog(c);
		kqsc_dl.requestWindowFeature(Window.FEATURE_LEFT_ICON);
		kqsc_dl.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		kqsc_dl.setTitle(select);
		name = ((MyApp) mApplication).getNameTV();

		if (select.equals("Soi Cầu")) {
			kqsc_dl.setContentView(R.layout.dialog_caulo);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);

			tv2socuoiDb = (TextView) kqsc_dl.findViewById(R.id.tv2socuoiDb);
			tvBovenhieu = (TextView) kqsc_dl.findViewById(R.id.tvBovenhieu);
			tvLoto3so = (TextView) kqsc_dl.findViewById(R.id.tvLoto3so);
			tvTkbovenhieu = (TextView) kqsc_dl.findViewById(R.id.tvTkbovenhieu);
			tvtkCau = (TextView) kqsc_dl.findViewById(R.id.tvTkCau);

			sendrequest("SC");

		} else if (select.equals("Cặp Số Đẹp")) {
			kqsc_dl.setContentView(R.layout.dialog_capsodep);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvCau10ngay = (TextView) kqsc_dl.findViewById(R.id.tvCau10ngay);
			tvCau9ngay = (TextView) kqsc_dl.findViewById(R.id.tvCau9ngay);
			tvCau8ngay = (TextView) kqsc_dl.findViewById(R.id.tvCau8ngay);
			tvCau7ngay = (TextView) kqsc_dl.findViewById(R.id.tvCau7ngay);
			tvLuachon = (TextView) kqsc_dl.findViewById(R.id.tvLuachon);
			tvChuy = (TextView) kqsc_dl.findViewById(R.id.tvChuy);

			sendrequest("CSD");
		} else if (select.equals("Lô Bạch Thủ")) {
			kqsc_dl.setContentView(R.layout.dialog_lobachthu);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvLBT = (TextView) kqsc_dl.findViewById(R.id.tvLBT);
			tvChuy = (TextView) kqsc_dl.findViewById(R.id.tvChuy);
			sendrequest("LBT");

			tvDateDetail.setText("Lô Bạch Thủ Miền Bắc, ngày *" + date_current);
		} else if (select.equals("Lô Xiên")) {
			kqsc_dl.setContentView(R.layout.dialog_loxien);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvXien2 = (TextView) kqsc_dl.findViewById(R.id.tvXien2);
			tvXien3 = (TextView) kqsc_dl.findViewById(R.id.tvXien3);
			tvXien4 = (TextView) kqsc_dl.findViewById(R.id.tvXien4);
			sendrequest("LX");
		} else if (select.equals("Lô Xiên Quay")) {
			kqsc_dl.setContentView(R.layout.dialog_loxienquay);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvInfo = (TextView) kqsc_dl.findViewById(R.id.tvInfo);
			tvTenLo = (TextView) kqsc_dl.findViewById(R.id.tvTenLo);
			sendrequest("LXQ");
		} else if (select.equals("Lô Âm Dương")) {
			kqsc_dl.setContentView(R.layout.dialog_loxienquay);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvInfo = (TextView) kqsc_dl.findViewById(R.id.tvInfo);
			tvTenLo = (TextView) kqsc_dl.findViewById(R.id.tvTenLo);
			sendrequest("LAM");
		} else if (select.equals("Lô Ngũ Hành")) {
			kqsc_dl.setContentView(R.layout.dialog_loxienquay);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvInfo = (TextView) kqsc_dl.findViewById(R.id.tvInfo);
			tvTenLo = (TextView) kqsc_dl.findViewById(R.id.tvTenLo);
			sendrequest("LNH");
		} else if (select.equals("Dàn Đặc Biệt Ngày")) {
			kqsc_dl.setContentView(R.layout.dialog_dandacbiet);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvInfo = (TextView) kqsc_dl.findViewById(R.id.tvInfo);
			tvTenLo = (TextView) kqsc_dl.findViewById(R.id.tvTenLo);
			sendrequest("DDBN");
		} else if (select.equals("Dàn Đặc Biệt Tuần")) {
			kqsc_dl.setContentView(R.layout.dialog_dandacbiet);
			tvDateDetail = (TextView) kqsc_dl.findViewById(R.id.tvDateDetail);
			tvNguhanh = (TextView) kqsc_dl.findViewById(R.id.tvNguhanh);
			tvHoangdao = (TextView) kqsc_dl.findViewById(R.id.tvHoangdao);
			tvInfo = (TextView) kqsc_dl.findViewById(R.id.tvInfo);
			tvTenLo = (TextView) kqsc_dl.findViewById(R.id.tvTenLo);
			sendrequest("DDBT");
		}

		tvDkVip = (TextView) kqsc_dl.findViewById(R.id.tvDkVip);
		tvDkVip.setText(Html.fromHtml("<p><u>Click DK Vip nhận tin mỗi ngày(5000d)</u></p>"));
		tvDkVip.setVisibility(isFree ? View.GONE : View.VISIBLE);
		
		tvNguhanh.setText("" + strNguhanh);
		tvHoangdao.setText("Giờ Hoàng Đạo: " + strHoangDao);
		kqsc_dl.getWindow().setTitleColor(XoSoConstants.Yellow1);
		kqsc_dl.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon_kqxs);
		pdia.dismiss();

		btCancel = (Button) kqsc_dl.findViewById(R.id.btCancel);
		btCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				kqsc_dl.dismiss();
			}
		});

		tvDkVip.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				// SQLITE DB
				db = new DataBaseHandler(getActivity());
				item_sub = db.getSubSv(code);
				if (item_sub != null) {
					System.out.println("======>> Run 1");
					System.out.println("DATA NEED ===>> " + item_sub.getCode()
							+ " ------ " + item_sub.getWeekofyear());
					System.out.println("DAY OF MONTH " + dayofmonth);
					if (dayofmonth == Integer.parseInt(item_sub.getWeekofyear())) {
						System.out.println("======>> Run 1--1");
						isSendSms = false;
					} else {
						System.out.println("======>> Run 2");
						db.updateSubSv(item_sub);
						isSendSms = true;
					}
				} else {
					System.out.println("======>> Run 3");
					item_sub = new SubSv_Item(code, Integer
							.toString(dayofmonth));
					db.insertSubSv(item_sub);
					isSendSms = true;
				}

				if (isSendSms) {
					if (nhamang.contains("VIETTEL")) {
//						if (nhamang.contains("VIETTEL")|| nhamang.contains("MOBIFONE")|| nhamang.contains("VN MobiFone")) {
						System.out.println("======>> Run 4");
						if (code.contains("XSTD")) {
							System.out.println("======>> Run 5");
							Util.sendSMS("XIEN MB", "8585", getActivity());
						} else {
							System.out.println("======>> Run 6");
							Util.sendSMS(
									"XIEN " + code.substring(2, code.length()),
									"8585", getActivity());
						}
						Util.SharedPreferencesSub(Integer.toString(dayofmonth),
								code, getActivity());
					} else {
						System.out.println("======>> Run 7");
						if (code.contains("XSTD")) {
							System.out.println("======>> Run 8");
							Util.sendSMS("DK MB", "8985", getActivity());
							Util.SharedPreferencesSub(
									Integer.toString(dayofmonth), "subxssvmb",
									getActivity());
						} else {
							if ((code.contains("XSBDH")
									|| code.contains("XSDLK")
									|| code.contains("XSDNG")
									|| code.contains("XSDNO")
									|| code.contains("XSGL")
									|| code.contains("XSKH")
									|| code.contains("XSKT")
									|| code.contains("XSNT")
									|| code.contains("XSPY")
									|| code.contains("XSQB")
									|| code.contains("XSQNI")
									|| code.contains("XSQNM")
									|| code.contains("XSQT") || code
									.contains("XSTTH"))) {
								Util.sendSMS("DK MT", "8985", getActivity());
								Util.SharedPreferencesSub(
										Integer.toString(dayofmonth),
										"subxssvmt", getActivity());
							} else {
								Util.sendSMS("DK MN", "8985", getActivity());
								Util.SharedPreferencesSub(
										Integer.toString(dayofmonth),
										"subxssvmn", getActivity());
							}
						}
					}
					Toast.makeText(getActivity(), "Đăng ký thành công!",
							Toast.LENGTH_LONG).show();
				} else {
					if (nhamang.contains("VIETTEL")) {
//					if (nhamang.contains("VIETTEL")
//							|| nhamang.contains("MOBIFONE")
//							|| nhamang.contains("VN MobiFone")) {
						System.out.println("======>> Run 4");
						if (Util.getdataSms(getActivity(), " SO VANG")
								&& !Util.getSharePreferencesSub(getActivity(),
										code).equals("1")) {
							if (code.contains("XSTD")) {
								System.out.println("======>> Run 5");
								Util.sendSMS("XIEN MB", "8585", getActivity());
							} else {
								System.out.println("======>> Run 6");
								Util.sendSMS(
										"XIEN "
												+ code.substring(2,
														code.length()), "8585",
										getActivity());
							}
							Util.SharedPreferencesSub(
									Integer.toString(dayofmonth), code,
									getActivity());
							Toast.makeText(getActivity(),
									"Đăng ký thành công!", Toast.LENGTH_LONG)
									.show();
						}
					} else {
						if (Util.getdataSms(getActivity(), " SO VANG")) {
							if (code.contains("XSTD")) {
								if (!Util.getSharePreferencesSub(getActivity(),
										"subxssvmb").equals("1")) {
									System.out.println("======>> Run 8");
									Util.sendSMS("DK MB", "8985",
											getActivity());
									Util.SharedPreferencesSub(
											Integer.toString(dayofmonth),
											"subtkmb", getActivity());
									Toast.makeText(getActivity(),
											"Đăng ký thành công!",
											Toast.LENGTH_LONG).show();
								}
							} else {
								if ((code.contains("XSBDH")
										|| code.contains("XSDLK")
										|| code.contains("XSDNG")
										|| code.contains("XSDNO")
										|| code.contains("XSGL")
										|| code.contains("XSKH")
										|| code.contains("XSKT")
										|| code.contains("XSNT")
										|| code.contains("XSPY")
										|| code.contains("XSQB")
										|| code.contains("XSQNI")
										|| code.contains("XSQNM")
										|| code.contains("XSQT") || code
										.contains("XSTTH"))) {
									if (!Util.getSharePreferencesSub(
											getActivity(), "subxssvmt").equals(
											"1")) {
										Util.SharedPreferencesSub("1",
												"subxssvmt", getActivity());
										Util.sendSMS("DK MT", "8985",
												getActivity());
										Toast.makeText(getActivity(),
												"Đăng ký thành công!",
												Toast.LENGTH_LONG).show();
									}
								} else {
									if (!Util.getSharePreferencesSub(
											getActivity(), "subxssvmn").equals(
											"1")) {
										Util.SharedPreferencesSub("1",
												"subxssvmn", getActivity());
										Util.sendSMS("DK MN", "8985",
												getActivity());
										Toast.makeText(getActivity(),
												"Đăng ký thành công!",
												Toast.LENGTH_LONG).show();
									}
								}
							}
						}
					}
				}
				// END SEND KQ
			}
		});
	}

	public void sendrequest(final String option) {
		pdia.show();
		AsyncTask<Void, Integer, Void> myTask = new AsyncTask<Void, Integer, Void>() {
			protected Void doInBackground(Void... params) {

				if (option.equals("SC")) {

					Sc_Item item = mPreference.getSoiCau(code);
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get soicau from preference");
						arraylist_sc = new ArrayList<Sc_Item>();
						arraylist_sc.add(item);
					} else {
						SmartLog.d("get soicau from server");
						arraylist_sc = parser_sc.getList(request.getkqsc("SC",
								code));
						// save soicau to preference
						if (arraylist_sc != null && arraylist_sc.size() > 0) {
							mPreference.setSoiCau(code, arraylist_sc.get(0));
						}
					}
				} else if (option.equals("CSD")) {

					Csd_Item item = mPreference.getSoiCauSoDep(code);
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get cau so dep from preference");
						arraylist_csd = new ArrayList<Csd_Item>();
						arraylist_csd.add(item);
					} else {
						arraylist_csd = parser_csd.getList(request.getkqsc(
								"CSD", code));
						// save soicau to preference
						if (arraylist_csd != null && arraylist_csd.size() > 0) {
							mPreference.setSoiCauSoDep(code,
									arraylist_csd.get(0));
						}
					}
				} else if (option.equals("LBT")) {

					Lbt_Item item = mPreference.getLoBachThu(code);
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get lo bach thu from preference");
						arraylist_lbt = new ArrayList<Lbt_Item>();
						arraylist_lbt.add(item);
					} else {
						arraylist_lbt = parser_lbt.getList(request.getkqsc(
								"LBT", code));
						// save lbt to preference
						if (arraylist_lbt != null && arraylist_lbt.size() > 0) {
							mPreference
									.setLoBachThu(code, arraylist_lbt.get(0));
						}
					}
				} else if (option.equals("LX")) {

					Lx_Item item = mPreference.getLoXien(code);
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get lo xien from preference");
						arraylist_lx = new ArrayList<Lx_Item>();
						arraylist_lx.add(item);
					} else {
						arraylist_lx = parser_lx.getList(request.getkqsc("LX",
								code));
						// save lx to preference
						if (arraylist_lx != null && arraylist_lx.size() > 0) {
							mPreference.setLoXien(code, arraylist_lx.get(0));
						}
					}
				} else if (option.equals("LXQ")) {

					LoXq_Item item = mPreference.getLoXienQuay(code, "LTXQ");
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get lo xien quay from preference");
						arraylist_lxq = new ArrayList<LoXq_Item>();
						arraylist_lxq.add(item);
					} else {
						arraylist_lxq = parser_lxq.getList(request.getkqsc(
								"LTXQ", code));
						// save lxq to preference
						if (arraylist_lxq != null && arraylist_lxq.size() > 0) {
							mPreference.setLoXienQuay(code, "LTXQ",
									arraylist_lxq.get(0));
						}
					}

				} else if (option.equals("LAM")) {

					LoXq_Item item = mPreference.getLoXienQuay(code, "LTAD");
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get lo am duong from preference");
						arraylist_lxq = new ArrayList<LoXq_Item>();
						arraylist_lxq.add(item);
					} else {
						arraylist_lxq = parser_lxq.getList(request.getkqsc(
								"LTAD", code));
						// save lxq to preference
						if (arraylist_lxq != null && arraylist_lxq.size() > 0) {
							mPreference.setLoXienQuay(code, "LTAD",
									arraylist_lxq.get(0));
						}
					}

				} else if (option.equals("LNH")) {

					LoXq_Item item = mPreference.getLoXienQuay(code, "LTNH");
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get lo ngu hang from preference");
						arraylist_lxq = new ArrayList<LoXq_Item>();
						arraylist_lxq.add(item);
					} else {
						arraylist_lxq = parser_lxq.getList(request.getkqsc(
								"LTNH", code));
						// save lo ngu hanh to preference
						if (arraylist_lxq != null && arraylist_lxq.size() > 0) {
							mPreference.setLoXienQuay(code, "LTNH",
									arraylist_lxq.get(0));
						}
					}

				} else if (option.equals("DDBN")) {

					LoXq_Item item = mPreference.getLoXienQuay(code, "DDBN");
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get dan dac biet ngay from preference");
						arraylist_lxq = new ArrayList<LoXq_Item>();
						arraylist_lxq.add(item);
					} else {
						arraylist_lxq = parser_lxq.getList(request.getkqsc(
								"DDBN", code));
						// save lo ngu hanh to preference
						if (arraylist_lxq != null && arraylist_lxq.size() > 0) {
							mPreference.setLoXienQuay(code, "DDBN",
									arraylist_lxq.get(0));
						}
					}
				} else if (option.equals("DDBT")) {

					LoXq_Item item = mPreference.getLoXienQuay(code, "DDBT");
					if (item != null && item.getDate().equals(date_current)) {
						SmartLog.d("get dan dac biet tuan from preference");
						arraylist_lxq = new ArrayList<LoXq_Item>();
						arraylist_lxq.add(item);
					} else {
						arraylist_lxq = parser_lxq.getList(request.getkqsc(
								"DDBT", code));
						// save lo ngu hanh to preference
						if (arraylist_lxq != null && arraylist_lxq.size() > 0) {
							mPreference.setLoXienQuay(code, "DDBT",
									arraylist_lxq.get(0));
						}
					}
				}
				return null;
			}

			protected void onPostExecute(Void result) {
				super.onPreExecute();
				if (option.equals("SC")) {
					if (arraylist_sc != null) {
						tvDateDetail.setText("Soi Cầu " + name + ", ngày *"
								+ date_current);
						tv2socuoiDb.setText(arraylist_sc.get(0).getDecham());
						tvBovenhieu.setText(arraylist_sc.get(0).getDudoande());
						tvLoto3so.setText(arraylist_sc.get(0).getLoto3so());
						tvTkbovenhieu
								.setText(arraylist_sc.get(0).getDudoanlo());
						tvtkCau.setText(arraylist_sc.get(0).getLuachon());
						pdia.dismiss();
					}
				} else if (option.equals("CSD")) {
					if (arraylist_csd != null) {
						tvDateDetail.setText("Cặp số đẹp " + name + ", ngày *"
								+ date_current);
						tvCau10ngay.setText(arraylist_csd.get(0).getCau10());
						tvCau9ngay.setText(arraylist_csd.get(0).getCau9());
						tvCau8ngay.setText(arraylist_csd.get(0).getCau8());
						tvCau7ngay.setText(arraylist_csd.get(0).getCau8());
						tvLuachon.setText(arraylist_csd.get(0).getLuachon());
					}
				} else if (option.equals("LBT")) {
					if (arraylist_lbt != null) {
						tvDateDetail.setText("Lô bạch thủ " + name + ", ngày *"
								+ date_current);
						tvLBT.setText(arraylist_lbt.get(0).getLuachon());
						tvChuy.setText(arraylist_lbt.get(0).getLuachon()
								+ arraylist_lbt.get(0).getLuachon()
										.substring(0, 1));
						System.out.println("LÔ BẠCH THỦ");
					}
				} else if (option.equals("LX")) {
					if (arraylist_lx != null) {
						tvDateDetail.setText("Lô Xiên "+name + ", ngày *"
								+ date_current);
						tvXien2.setText(arraylist_lx.get(0).getXien2());
						tvXien3.setText(arraylist_lx.get(0).getXien3());
						tvXien4.setText(arraylist_lx.get(0).getXien4());
					}
				} else if (option.equals("LXQ")) {
					if (arraylist_lxq != null) {
						tvDateDetail.setText("Lô Xiên Quay Miền Bắc, ngày *"
								+ date_current);
						tvTenLo.setText("Lô Xiên Quay cực kết hôm nay ");
						tvInfo.setText(arraylist_lxq.get(0).getInfo());
					}
				} else if (option.equals("LAM")) {
					if (arraylist_lxq != null) {
						tvDateDetail.setText("Lô Âm Dương Miền Bắc, ngày *"
								+ date_current);
						tvTenLo.setText("Lô Âm Dương cực kết hôm nay ");
						tvInfo.setText(arraylist_lxq.get(0).getInfo());
					}
				} else if (option.equals("LNH")) {
					if (arraylist_lxq != null) {
						tvDateDetail.setText("Lô Ngũ Hành Miền Bắc, ngày *"
								+ date_current);
						tvTenLo.setText("Lô Ngũ Hành cực kết hôm nay ");
						tvInfo.setText(arraylist_lxq.get(0).getInfo());
					}
				} else if (option.equals("DDBN")) {
					if (arraylist_lxq != null) {
						tvDateDetail
								.setText("Dàn Đặc Biệt Ngày Miền Bắc, ngày *"
										+ date_current);
						tvTenLo.setText("Dàn Đặc Biệt Ngày hôm nay ");
						tvInfo.setText(arraylist_lxq.get(0).getInfo()
								.replaceAll("<br/>", ""));
					}
				} else if (option.equals("DDBT")) {
					if (arraylist_lxq != null) {
						tvDateDetail
								.setText("Dàn Đặc Biệt Tuần Miền Bắc, ngày *"
										+ date_current);
						tvTenLo.setText("Dàn Đặc Biệt Tuần ");
						tvInfo.setText(arraylist_lxq.get(0).getInfo()
								.replaceAll("<br/>", ""));
					}
				}
			}
		};
		myTask.execute();
	}
}
